package net.entelijan.cobean.examples.showcase.table;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JOptionPane;

import net.entelijan.cobean.core.ColumnDesc;
import net.entelijan.cobean.core.ISingleSelectionListCobean;
import net.entelijan.cobean.data.literature.ILitService;
import net.entelijan.cobean.data.literature.LitAuthor;
import net.entelijan.cobean.table.DefaultSingleSelectionTableInitializer;
import net.entelijan.cobean.util.DateComparator;
import net.entelijan.cobean.util.DateConverter;
import net.entelijan.scf.IShowcase;

public class SortTableShowcase implements IShowcase<SortTablePanel> {

	private ILitService service = null;

	public SortTableShowcase() {
		super();
	}

	public void init(final SortTablePanel panel) {

		// Create an initializer
		DefaultSingleSelectionTableInitializer<LitAuthor> initializer = new DefaultSingleSelectionTableInitializer<LitAuthor>();

		// Configure the initializer
		// Add the component
		initializer.setCoTable(panel.getTable());
		// Add the column descriptions
		List<ColumnDesc> colDescs = createColumnDescriptions();
		initializer.setColumnDescs(colDescs);

		// Initialize the table and receive a component bean.
		// From now on the component bean will be your interface to the GUI.
		final ISingleSelectionListCobean<LitAuthor> cobean = initializer
				.initialize();

		// Create some testdate and set it into the component bean
		List<LitAuthor> data = this.service.loadAllAuthors();
		cobean.setValue(data);

		// Now a list of locals is displayed in your table

		// Add an action to the 'show selection' button
		panel.getShowSelectionButton().addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				JOptionPane.showMessageDialog(panel, cobean.getSelectedValue());
			}

		});
		// Add an action to the 'remove selected' button
		panel.getDeleteSelectionButton().addActionListener(
				new ActionListener() {

					@Override
					public void actionPerformed(ActionEvent e) {
						LitAuthor sel = cobean.getSelectedValue();
						if (sel != null) {
							List<LitAuthor> all = cobean.getValue();
							all.remove(sel);
							cobean.setValue(all);
						}
					}

				});

		// Select the first row
		panel.getSelect1SelectionButton().addActionListener(
				new ActionListener() {

					@Override
					public void actionPerformed(ActionEvent e) {
						LitAuthor item = cobean.getValue().get(0);
						cobean.setSelectedValue(item);
					}

				});

		// Select the third row
		panel.getSelect3SelectionButton().addActionListener(
				new ActionListener() {

					@Override
					public void actionPerformed(ActionEvent e) {
						LitAuthor item = cobean.getValue().get(2);
						cobean.setSelectedValue(item);
					}

				});

	}

	/**
	 * Create some column descriptions for java.util.Local
	 * 
	 * @return A list of column descriptions
	 */
	private List<ColumnDesc> createColumnDescriptions() {
		List<ColumnDesc> colDescs = new ArrayList<ColumnDesc>();
		{
			ColumnDesc colDesc = new ColumnDesc();
			colDesc.setPropertyName("name");
			colDesc.setHeaderName("Name(sortable)");
			colDesc.setSortable(true);
			colDescs.add(colDesc);
		}
		{
			ColumnDesc colDesc = new ColumnDesc();
			colDesc.setPropertyName("language");
			colDesc.setHeaderName("Language(sortable)");
			colDesc.setSortable(true);
			colDescs.add(colDesc);
		}
		{
			ColumnDesc colDesc = new ColumnDesc();
			colDesc.setPropertyName("country");
			colDesc.setHeaderName("Country(sortable)");
			colDesc.setSortable(true);
			colDescs.add(colDesc);
		}
		{
			ColumnDesc colDesc = new ColumnDesc();
			colDesc.setPropertyName("birthday");
			colDesc.setHeaderName("Birthday (sortable)");
			colDesc.setSortable(true);
			colDesc.setComparator(new DateComparator());
			colDesc.setConverter(new DateConverter("yyyy-MM-dd"));
			colDescs.add(colDesc);
		}
		return colDescs;
	}

	public ILitService getService() {
		return service;
	}

	public void setService(ILitService service) {
		this.service = service;
	}

}
